//
//  RootTableViewCell.m
//  ioshuanwu
//
//  Created by 幻音 on 15/12/28.
//  Copyright © 2015年 幻音. All rights reserved.
//

#import "RootTableViewCell.h"
#import "Masonry.h"
@implementation RootTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
        
    }
    return self;
}
- (void)createView {
    _backImage = [[UIImageView alloc] init];
    [self.contentView addSubview:_backImage];
    _title = [[UILabel alloc] init];
    [self.contentView addSubview:_title];
    _title.font = [UIFont fontWithName:@"FZKaTong-M19S" size:9];
    _title.numberOfLines = 0;
    _originLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_originLabel];
    _originLabel.font = [UIFont fontWithName:@"FZKaTong-M19S" size:10];;
    _playButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:_playButton];
    _timeImage = [[UIImageView alloc] init];
    [self.contentView addSubview:_timeImage];
    _timeLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_timeLabel];
    _timeLabel.font = [UIFont fontWithName:@"FZKaTong-M19S" size:10];;
    _playCountImage = [[UIImageView alloc] init];
    [self.contentView addSubview:_playCountImage];
    _playCountLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_playCountLabel];
    _playCountLabel.font = [UIFont fontWithName:@"FZKaTong-M19S" size:10];;
    _joinInCommendImage  = [[UIImageView alloc] init];
    [self.contentView addSubview:_joinInCommendImage];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [_backImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(0);
        make.left.equalTo(self.contentView).offset(0);
        make.right.equalTo(self.contentView).offset(0);
        make.bottom.equalTo(self.contentView).offset(- 45);
    }];
    
    [_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backImage.mas_bottom).offset(5);
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(10);
    }];
    [_originLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.title.mas_bottom).offset(5);
        make.left.equalTo(self.contentView).offset(10);
        make.height.mas_equalTo(16);
        
    }];
    [_playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backImage.mas_centerX).offset(0);
        make.centerY.equalTo(self.backImage.mas_centerY).offset(0);
        make.width.mas_equalTo(64);
        make.height.mas_equalTo(64);
        
    }];
    [_timeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.originLabel.mas_right).offset(10);
        make.top.equalTo(self.title.mas_bottom).offset(7);
        make.height.mas_equalTo(11);
        make.width.mas_equalTo(11);
    }];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeImage.mas_right).offset(7);
        make.top.equalTo(self.title.mas_bottom).offset(5);
        make.height.mas_equalTo(16);
        
    }];
    [_playCountImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeLabel.mas_right).offset(10);
        make.top.equalTo(self.title.mas_bottom).offset(7);
        make.height.mas_equalTo(11);
        make.width.mas_equalTo(11);
        
    }];
    [_playCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_playCountImage.mas_right).offset(10);
        make.top.equalTo(self.title.mas_bottom).offset(5);
        make.height.mas_equalTo(16);
        
    }];
    [_joinInCommendImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(- 20);
        make.bottom.equalTo(self.contentView).offset(- 4);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(15);
        
    }];
}
- (void)setVideo:(Video *)video
{
    self.title.text = video.title;
    self.descriptionLabel.text = video.descriptionDe;
    [self.backImage sd_setImageWithURL:[NSURL URLWithString:video.cover] placeholderImage:[UIImage imageNamed:@"logo"]];
    self.playCountLabel.text = [NSString stringWithFormat:@"%@",video.commentTotalCount];
    int a = [[[NSString stringWithFormat:@"%@",video.duration] substringWithRange:NSMakeRange(0, 3)] intValue];
    int b = 60;
    int minute = a / b;
    int second = a % b + 0.5;
    NSString *time;
    if (a / b > 9) {
        time = [NSString stringWithFormat:@"%d:%d",minute,second];
    } else {
        time = [NSString stringWithFormat:@"0%d:%d",minute,second];
    }
    _timeLabel.text = time;
    self.originLabel.text = video.origin;
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    self.playCountImage.image = [UIImage imageNamed:@"icon_comment"];
    self.timeImage.image = [UIImage imageNamed:@"icon_time"];
    self.joinInCommendImage.image = [UIImage imageNamed:@"icon_joinInCommend"];
}

- (UILabel *)descriptionLabel
{
    if (_descriptionLabel == nil) {
        self.descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,160,320,40)];
        self.descriptionLabel.textColor = [UIColor grayColor];
        self.descriptionLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:_descriptionLabel];
    }
    return _descriptionLabel;
}

- (UIButton *)replyButton
{
    if (_replyButton == nil) {
        self.replyButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.replyButton.frame = CGRectMake(CGRectGetMaxX(self.playerView.frame) - 30, CGRectGetMinY(self.playCountLabel.frame), 15, 15);
        [self addSubview:_replyButton];
    }
    return _replyButton;
}

- (UIButton *)shareButton
{
    if (_shareButton == nil) {
        self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.replyButton.frame  = CGRectMake(CGRectGetMaxX(self.replyButton.frame), CGRectGetMinY(self.replyButton.frame), CGRectGetWidth(self.replyButton.frame), CGRectGetHeight(self.replyButton.frame));
        [self.contentView addSubview:_shareButton];
    }
    return _shareButton;
}


- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}




@end
